﻿@*******************************************************************************************************
//  Pages.cshtml - Gbtc
//
//  Copyright © 2016, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
//  file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://opensource.org/licenses/MIT
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  02/18/2016 - Ritchie Carroll
//       Generated original version of source code.
//
//*****************************************************************************************************@
@using GSF.Web.Model
@using PQDashboard.Model
@using Page = PQDashboard.Model.Page
@model AppModel
@{
    DataContext dataContext = Model.DataContext;

    ViewBag.HeaderColumns = new[]
    {   //    { "Field", "Label", "Classes" }
        new[] { "ID", "ID", "text-center" },
        new[] { "Name", "&nbsp;&nbsp;&nbsp;Name", "text-left" },
        new[] { "PageLocation", "URL", "text-left" },
        new[] { "Title", "Title", "text-left" }
    };

    ViewBag.CustomTitle = "Edit System Pages";
    ViewBag.Title = "System Page";
    ViewBag.BodyRows = BodyRows().ToString();
    ViewBag.AddNewEditDialog = AddNewEditDialog(dataContext).ToString();
}
@helper BodyRows()
{
    <td width="10%" data-bind="text: ID" class="text-center valign-middle" nowrap></td>
    <td width="15%" class="text-left valign-middle" nowrap><button type="button" class="btn btn-link" data-bind="text: Name, click: $parent.viewPageRecord"></button></td>
    <td width="30%" class="text-left table-cell-hard-wrap"><div data-bind="text: $($element.parentElement).truncateToWidth(Title, 1.5), attr: {title: Title}"></div></td>
    <td width="40%" class="text-left table-cell-hard-wrap"><a data-bind="attr:{href: AbsolutePath()}" target="_blank"><div data-bind="text: $($element.parentElement.parentElement).truncateToWidth(PageLocation, 2)"></div></a></td>
    <td width="5%" class="text-center valign-middle" nowrap>
        <button type="button" class="btn btn-xs" data-bind="click: $parent.editPageRecord, enable: $parent.dataHubIsConnected"><span class="glyphicon glyphicon-pencil"></span></button>
        <button type="button" class="btn btn-xs" data-bind="click: $parent.removePageRecord, enable: $parent.dataHubIsConnected"><span class="glyphicon glyphicon-remove"></span></button>
    </td>
}
@helper AddNewEditDialog(DataContext dataContext)
{
    <div class="col-md-6">
        @Html.Raw(dataContext.AddInputField<Page>("ID"))
        @Html.Raw(dataContext.AddInputField<Page>("Name"))
        @Html.Raw(dataContext.AddInputField<Page>("Title"))
        @Html.Raw(dataContext.AddSelectField<Page, Menu>("MenuID", "ID", "Name", "Name"))
        @Html.Raw(dataContext.AddInputField<Page>("PageLocation", "url"))
        @Html.Raw(dataContext.AddTextAreaField<Page>("ServerConfiguration"))
    </div>
    <div class="col-md-6">
        @Html.Raw(dataContext.AddTextAreaField<Page>("ClientConfiguration"))
        @Html.Raw(dataContext.AddInputField<Page>("IncludedRoles"))
        @Html.Raw(dataContext.AddInputField<Page>("ExcludedRoles"))
        @Html.Raw(dataContext.AddTextAreaField<Page>("Description", 3))
        @Html.Raw(dataContext.AddCheckBoxField<Page>("Enabled"))
    </div>
}
@Html.Partial("PagedViewModel")
@section Scripts {
    <script>
        @Html.Raw(Model.RenderViewModelConfiguration<Page>(ViewBag))
        function getAbsolutePath(record) {
            if (viewModel.dataHubIsConnected()) {
                dataHub.getAbsolutePath(notNull(record.PageLocation)).done(function (absolutePath) {
                    record.AbsolutePath(absolutePath);
                });
            }
        }

        $(viewModel).on("pageRecordsQueried", function(event, records) {
            // Add a virtual observable field called "AbsolutePath" that will resolve virtual path to absolute path
            for (var i = 0; i < records.length; i++) {
                records[i].AbsolutePath = ko.observable("...");
                getAbsolutePath(records[i]);
            }
        });

        $(viewModel).on("newRecord", function(event, newRecord) {
            // Make sure new records get virtual field
            newRecord.AbsolutePath = ko.observable("");
        });

        $(viewModel).on("recordSaved", function(event, observableRecord) {
            // Remove virtual observable fields before serialization for record updates
            delete observableRecord.AbsolutePath;
        });

    </script>
}